<template>
  <div class="donor-thanks-page">
    <div class="container">
      <h1 class="page-title">感谢捐赠者</h1>
      
      <el-card class="thanks-card">
        <template #header>
          <h2>特别感谢</h2>
        </template>
        
        <div class="thanks-content">
          <p class="thanks-message">
            感谢所有支持KBB服务器的玩家！正是因为你们的慷慨捐赠，我们才能持续改进服务器，
            为所有玩家提供更好的游戏体验。每一位捐赠者都是我们社区的重要成员。
          </p>
          
          <div class="donor-stats">
            <el-row :gutter="20">
              <el-col :xs="24" :sm="8" v-for="stat in donorStats" :key="stat.id">
                <div class="stat-item">
                  <div class="stat-number">{{ stat.value }}</div>
                  <div class="stat-label">{{ stat.label }}</div>
                </div>
              </el-col>
            </el-row>
          </div>
        </div>
      </el-card>
      
      <el-card class="donor-list-card">
        <template #header>
          <div class="card-header">
            <h2>捐赠者名单</h2>
            <el-input
              v-model="searchQuery"
              placeholder="搜索捐赠者"
              style="width: 200px;"
              clearable
            >
              <template #prefix>
                <el-icon><Search /></el-icon>
              </template>
            </el-input>
          </div>
        </template>
        
        <el-table 
          :data="filteredDonors" 
          style="width: 100%"
          :default-sort="{ prop: 'amount', order: 'descending' }"
          v-loading="loading"
        >
          <el-table-column prop="name" label="捐赠者" width="150" sortable />
          <el-table-column prop="amount" label="捐赠金额" width="120" sortable>
            <template #default="scope">
              <span class="amount">¥{{ scope.row.amount }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="date" label="捐赠日期" width="120" sortable />
          <el-table-column prop="message" label="留言" />
          <el-table-column label="等级" width="100">
            <template #default="scope">
              <el-tag :type="getDonorLevel(scope.row.amount).type">
                {{ getDonorLevel(scope.row.amount).label }}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
        
        <div class="pagination-wrapper">
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[10, 20, 50]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
      
      <el-card class="donor-levels-card">
        <template #header>
          <h2>捐赠等级说明</h2>
        </template>
        
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :md="6" v-for="level in donorLevels" :key="level.id">
            <div class="level-item">
              <div class="level-icon">
                <el-icon :size="40" :color="level.color">
                  <component :is="level.icon" />
                </el-icon>
              </div>
              <h3>{{ level.name }}</h3>
              <p class="level-amount">¥{{ level.minAmount }} - ¥{{ level.maxAmount }}</p>
              <ul class="level-benefits">
                <li v-for="benefit in level.benefits" :key="benefit">{{ benefit }}</li>
              </ul>
            </div>
          </el-col>
        </el-row>
      </el-card>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import { Search, Star, Medal, Trophy, Crown } from '@element-plus/icons-vue'
import { getDonorData } from '../api/serverApi'

export default {
  name: 'DonorThanksPage',
  components: {
    Search,
    Star,
    Medal,
    Trophy,
    Crown
  },
  setup() {
    const donors = ref([])
    const loading = ref(false)
    const searchQuery = ref('')
    const currentPage = ref(1)
    const pageSize = ref(10)
    
    const donorStats = ref([
      {
        id: 1,
        label: '总捐赠金额',
        value: '¥12,580'
      },
      {
        id: 2,
        label: '捐赠者数量',
        value: '156'
      },
      {
        id: 3,
        label: '平均捐赠',
        value: '¥80.6'
      }
    ])
    
    const donorLevels = ref([
      {
        id: 1,
        name: '铜牌捐赠者',
        minAmount: 10,
        maxAmount: 49,
        color: '#CD7F32',
        icon: 'Star',
        benefits: ['专属称号', '特殊聊天颜色', '优先进入服务器']
      },
      {
        id: 2,
        name: '银牌捐赠者',
        minAmount: 50,
        maxAmount: 199,
        color: '#C0C0C0',
        icon: 'Medal',
        benefits: ['铜牌所有福利', '更多传送点', '专属皮肤']
      },
      {
        id: 3,
        name: '金牌捐赠者',
        minAmount: 200,
        maxAmount: 499,
        color: '#FFD700',
        icon: 'Trophy',
        benefits: ['银牌所有福利', 'VIP聊天频道', '优先客服支持']
      },
      {
        id: 4,
        name: '钻石捐赠者',
        minAmount: 500,
        maxAmount: 999999,
        color: '#B9F2FF',
        icon: 'Crown',
        benefits: ['金牌所有福利', '活动优先参与', '专属管理员']
      }
    ])
    
    const filteredDonors = computed(() => {
      let filtered = donors.value
      if (searchQuery.value) {
        filtered = filtered.filter(donor => 
          donor.name.toLowerCase().includes(searchQuery.value.toLowerCase()) ||
          donor.message.toLowerCase().includes(searchQuery.value.toLowerCase())
        )
      }
      
      const start = (currentPage.value - 1) * pageSize.value
      const end = start + pageSize.value
      return filtered.slice(start, end)
    })
    
    const total = computed(() => {
      if (searchQuery.value) {
        return donors.value.filter(donor => 
          donor.name.toLowerCase().includes(searchQuery.value.toLowerCase()) ||
          donor.message.toLowerCase().includes(searchQuery.value.toLowerCase())
        ).length
      }
      return donors.value.length
    })
    
    const getDonorLevel = (amount) => {
      const level = donorLevels.value.find(l => 
        amount >= l.minAmount && amount <= l.maxAmount
      )
      return {
        label: level ? level.name : '普通玩家',
        type: level ? 'success' : 'info'
      }
    }
    
    const handleSizeChange = (val) => {
      pageSize.value = val
      currentPage.value = 1
    }
    
    const handleCurrentChange = (val) => {
      currentPage.value = val
    }
    
    const loadData = async () => {
      loading.value = true
      try {
        donors.value = getDonorData()
      } catch (error) {
        console.error('加载捐赠者数据失败:', error)
      } finally {
        loading.value = false
      }
    }
    
    onMounted(() => {
      loadData()
    })
    
    return {
      donors,
      loading,
      searchQuery,
      currentPage,
      pageSize,
      donorStats,
      donorLevels,
      filteredDonors,
      total,
      getDonorLevel,
      handleSizeChange,
      handleCurrentChange
    }
  }
}
</script>

<style scoped>
.donor-thanks-page {
  padding: 20px 0;
}

.page-title {
  font-size: 2.5rem;
  font-weight: bold;
  text-align: center;
  margin-bottom: 40px;
  color: #4CAF50;
}

.thanks-card {
  margin-bottom: 30px;
}

.thanks-card h2 {
  color: #4CAF50;
  margin: 0;
}

.thanks-message {
  font-size: 1.1rem;
  line-height: 1.8;
  color: #666;
  text-align: center;
  margin-bottom: 30px;
}

.donor-stats {
  margin-top: 30px;
}

.stat-item {
  text-align: center;
  padding: 20px;
  background: #f9f9f9;
  border-radius: 8px;
}

.stat-number {
  font-size: 2rem;
  font-weight: bold;
  color: #4CAF50;
  margin-bottom: 10px;
}

.stat-label {
  color: #666;
  font-size: 1rem;
}

.donor-list-card {
  margin-bottom: 30px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h2 {
  color: #4CAF50;
  margin: 0;
}

.amount {
  font-weight: bold;
  color: #4CAF50;
}

.pagination-wrapper {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.donor-levels-card h2 {
  color: #4CAF50;
  margin: 0;
}

.level-item {
  text-align: center;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 8px;
  height: 100%;
}

.level-icon {
  margin-bottom: 15px;
}

.level-item h3 {
  color: #4CAF50;
  margin-bottom: 10px;
}

.level-amount {
  color: #666;
  font-weight: bold;
  margin-bottom: 15px;
}

.level-benefits {
  list-style: none;
  padding: 0;
  text-align: left;
}

.level-benefits li {
  margin-bottom: 8px;
  padding-left: 20px;
  position: relative;
  color: #666;
}

.level-benefits li::before {
  content: "✓";
  position: absolute;
  left: 0;
  color: #4CAF50;
  font-weight: bold;
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }
  
  .card-header {
    flex-direction: column;
    gap: 15px;
  }
  
  .stat-item {
    margin-bottom: 15px;
  }
  
  .level-item {
    margin-bottom: 20px;
  }
}
</style> 